package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl  implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 保存菜品
     * @param dto
     */
    @Override
    @Transactional // 开启事务
    public void save(DishDTO dto) {
        // 1) 向dish 表保存数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dto,dish); // 复制属性
        dish.setStatus(StatusConstant.DISABLE); // 默认停售状态
        dishMapper.save(dish); // 保存 菜品的主键会自增  ,开启主键自增功能会返回

        //2) 口味表保存数据
        List<DishFlavor> flavors = dto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dish.getId());
        }
        // 批量保存
        if(flavors!=null&& flavors.size()>0){
            dishFlavorMapper.insertBatch(flavors);
        }


    }
    /**
     * 分页查询
     * @param dto
     * @return
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dto) {
        // 1)设置分页条件
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        //2) 条件查询
        Page<DishVO> page  = dishMapper.page(dto);
        //3)  封装返回
        return new PageResult(page.getTotal(), page.getResult());
    }
    /**
     * 删除
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public void deleteByIds(List<Long> ids) {
        if(ids==null|| ids.size()==0){
            return;
        }
        // 校验
        for (Long id : ids) {
             // 1) 查询每个菜品
             Dish dish =   dishMapper.getById(id);
                // 2) 判读是否起售
             if(dish.getStatus()==StatusConstant.ENABLE){
                 // 抛出业务异常
                 throw   new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
             }
             // 3)查询是否关联了套餐
             List<Long> setmealIds =   setmealDishMapper.getSetmealIdsByDishId(id);
             if(setmealIds!=null&&setmealIds.size()>0){
                 // 抛出业务异常
                 throw   new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
             }

        }
        // 删除
        for (Long id : ids) {
            // 4) 删除菜品
            dishMapper.deleteById(id);
            // 5) 删除口味
            dishFlavorMapper.deleteByDishID(id);// 根据菜品id 删除 口味
        }
    }
    // 根据菜品id 查询菜品及口味
    @Override
    public DishVO getById(Long id) {
        // 1) 查询菜品信
        Dish dish = dishMapper.getById(id);
        // 2) 查询口味信息(根据菜品id)
        List<DishFlavor> flavors = dishFlavorMapper.getFlavorsByDishId(id);
        // 3) 封装返回
        DishVO dishVO = new DishVO();
        if(dish==null){
            return dishVO;
        }
        dishVO.setFlavors(flavors);
        BeanUtils.copyProperties(dish,dishVO);

        return dishVO;
    }
    // 根据菜品id 查询菜品及口味 : 第二种方式
    @Override
    public DishVO getById2(Long id) {
      DishVO vo = dishMapper.getDishWithFlavorById(id);
      return vo ;
    }



    //更新
    @Transactional
    @Override
    public void update(DishDTO dto) {
        // 1 更新菜品数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dto,dish);
        dishMapper.update(dish); //更新
        // 2删除口味信息
        dishFlavorMapper.deleteByDishID(dish.getId());
        // 3新增口味
        List<DishFlavor> flavors = dto.getFlavors();
        if(flavors!=null&&flavors.size()>0){
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dish.getId()); //设置菜品id
            }
            // 批量插入
            dishFlavorMapper.insertBatch(flavors);
        }
    }


    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    public List<Dish> list(Long categoryId) {

        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();

        return dishMapper.list(dish);
    }

    /**
     * 起停售
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        Dish dish = Dish.builder()
                .id(id)
                .status(status)
                .build();
        dishMapper.update(dish);
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getFlavorsByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

}
